/**
 * <p>Title: Snake</p>
 * <p>Copyright: (C) 2011 Gavin's Snake project. Licensed under the Apache License, Version 2.0 (the "License")</p>
 * @author Gavin
 */

package com.deaboway.snake.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.RecordStore;

public class Bundle extends BaseRMS {
	private static String[] SECTION = {
		"\"AL\":","\"DT\":",
		"\"ND\":","\"MD\":",
		"\"SC\":","\"ST\":"};
	private static int LEN = SECTION.length;
	
	private static boolean inited = false;
	private static Bundle INSTANCE;
	public static void INIT(){
		if(inited)return;
		inited = true;
		INSTANCE = new Bundle();
		INSTANCE.loadBundles();
	}
	
	public static Bundle getInstance(){
		return INSTANCE;
	}

	private String[] CONTENT = new String[LEN];
	private Bundle() {
		super("snake-view");
	}

	public void loadBundles() {
		try {
			this.open();
			this.close();
		} catch (Exception e) {
			try {
				this.close();
				RecordStore.deleteRecordStore(this.getRMSName());

				this.open();
				this.close();
			} catch (Exception ex) {
			}

		}
	}

	public void resetBundles() {
			try {
				this.close();
				RecordStore.deleteRecordStore(this.getRMSName());

				this.open();
				this.close();
			} catch (Exception ex) {
			}
	}

	public void updateBundles() throws Exception {
		try {
			this.openonly();

			updateData();
			if (this.getRecordStore() != null)
				this.close();
		} catch (Exception e) {
			throw new Exception(this.getRMSName() + "::updateBundles::" + e);
		}
	}

	protected void loadData() throws Exception {
		try {
			byte[] record = this.getRecordStore().getRecord(1);
			DataInputStream istream = new DataInputStream(
					new ByteArrayInputStream(record, 0, record.length));
			String content = istream.readUTF();
			int[] start = new int[LEN+1];
			for(int i =0;i<LEN;i++){
				start[i] = content.indexOf(SECTION[i]);
			}
			start[LEN]=content.length();
			for(int i =0;i<LEN;i++){
				CONTENT[i] = content.substring(start[i]+5,start[i+1]);
				Log.info("CONTENT["+i+"]="+CONTENT[i]);
			}
		} catch (Exception e) {
			throw new Exception(this.getRMSName() + "::loadData::" + e);
		}
	}

	protected void createDefaultData() throws Exception {
		try {
			ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
			DataOutputStream ostream = new DataOutputStream(bstream);
			
			CONTENT[0] = "9,20,9,7";
			CONTENT[1] = "1";
			CONTENT[2] = "1";
			CONTENT[3] = "600";
			CONTENT[4] = "0";
			CONTENT[5] = "7,7,6,7,5,7,4,7,3,7,2,7";
			
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<LEN;i++){
				sb.append(SECTION[i]);
				sb.append(CONTENT[i]);
			}
			
			ostream.writeUTF( sb.toString());

			ostream.flush();
			ostream.close();
			byte[] record = bstream.toByteArray();
			this.getRecordStore().addRecord(record, 0, record.length);
		} catch (Exception e) {
			throw new Exception(this.getRMSName() + "::createDefaultData::" + e);
		}
	}

	protected void updateData() throws Exception {
		try {
			ByteArrayOutputStream bstream = new ByteArrayOutputStream(12);
			DataOutputStream ostream = new DataOutputStream(bstream);
			StringBuffer sb = new StringBuffer();
			for(int i=0;i<LEN;i++){
				sb.append(SECTION[i]);
				sb.append(CONTENT[i]);
			}
			ostream.writeUTF(sb.toString());

			ostream.flush();
			ostream.close();
			byte[] record = bstream.toByteArray();
			this.getRecordStore().setRecord(1, record, 0, record.length);
		} catch (Exception e) {
			throw new Exception(this.getRMSName() + "::updateData::" + e);
		}
	}
	
	public String getValue(int key) {
		return CONTENT[key];
	}

	public void setValue(int key, String value) {
		CONTENT[key] = value;
	}
}
